package com.config.interceptorConfig;

import com.interceptor.AccessInterceptor;
import com.interceptor.AccessSpringInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class AccessInteceptorConfig implements WebMvcConfigurer {

    @Bean
    public AccessInterceptor getAccessInterceptor(){
        return new AccessInterceptor();
    }

    @Bean
    public AccessSpringInterceptor getAccessSpringInterceptor() {
        return new AccessSpringInterceptor();
    }

    public static List<String> excludePath;
    static {
        excludePath = new ArrayList<>();
        excludePath.add("/*/*.*");
        excludePath.add("/*.*");
        excludePath.add("/");
        excludePath.add("/test");
        excludePath.add("/error/common*");
        excludePath.add("/error");

        excludePath.add("/user/doLogin*");
        excludePath.add("/record/pushEquipCollectRecord*");
        excludePath.add("/supervise/*");
        excludePath.add("/auth/downloadImage*");
        excludePath.add("/equip/getMyEquipById");
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getAccessSpringInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(getAccessInterceptor()).addPathPatterns("/**").excludePathPatterns(excludePath);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry){
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .maxAge(3600)
                .allowCredentials(true);
    }
}
